package com.szqhyxj.youxiangju.biz.merchantHome.shop.site;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.adapter.MySiteAdapter;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.addSite.AddSiteActivity;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的地址
 */
public class MySiteActivity extends BaseActivity implements NavigationBar.NavigationBarListener,MySiteContract.View, MySiteAdapter.OnClickListener, MsgChoicePopView.OnClickListener{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.no_site_prompt)
    LinearLayout noSitePrompt;
    @BindView(R.id.site_list)
    ListView siteList;
    private MySiteAdapter adapter;
    private MySitePresenter presenter;
    private List<DefaultSite> sites = new ArrayList<>();
    private String addressId;
    public static final int START_100 = 100 ;
    public static void start(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity, MySiteActivity.class);
        activity.startActivityForResult(intent,START_100);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_site);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        presenter = new MySitePresenter(this);

        adapter = new MySiteAdapter(this,sites,this);
        siteList.setAdapter(adapter);

        initEvent();
    }

    private void initEvent(){
        siteList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                DefaultSite site = sites.get(i);
                navSetTakeSite(site.getAddrId()+"");
            }
        });
    }


    @OnClick(R.id.add_site)
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.add_site:
                AddSiteActivity.start(this);
                break;
        }
    }

    @Override
    protected void onResume(){
        super.onResume();
        presenter.init();
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }

    @Override
    public void showSiteList(List<DefaultSite> sites){
        noSitePrompt.setVisibility(View.GONE);
        siteList.setVisibility(View.VISIBLE);
        this.sites.clear();
        this.sites.addAll(sites);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showNoSite(){
        siteList.setVisibility(View.GONE);
        noSitePrompt.setVisibility(View.VISIBLE);
    }

    @Override
    public void showRefresh(){
        presenter.init();
    }

    @Override
    public void navAddSite(){
        AddSiteActivity.start(this);
    }

    @Override
    public void navEditDddress(DefaultSite defaultSite){
        AddSiteActivity.start(this,defaultSite);
    }

    @Override
    public void navSetTakeSite(String siteId){
        Intent intent = new Intent();
        intent.putExtra("addrId",siteId);
        setResult(START_100,intent);
        finish();
    }

    @Override
    public void deleteDddress(String addressId){
        this.addressId = addressId;
        MsgChoicePopView popView = new MsgChoicePopView(this);
        popView.setOnClickListener(this);
        popView.setMsg("是否删除当前地址");
        popView.show();
    }

    @Override
    public void editDddress(DefaultSite defaultSite){
        presenter.editDddress(defaultSite);
    }

    @Override
    public void setDefaults(String addressId){
        presenter.setDefaults(addressId);
    }

    @Override
    public void onClickConfirm(View v){
        presenter.deleteDddress(addressId);
    }
}
